package thread_case;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPoolExecutor1 {
    private BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue(1000);
    private List<Thread> threadList = new ArrayList<>();
    public MyThreadPoolExecutor1(int n) {
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                while (true) {
                    try {
                        Runnable runnable = blockingQueue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
            threadList.add(t);
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        blockingQueue.put(runnable);
    }
}

public class ThreadDemo6 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor1 myThreadPoolExecutor = new MyThreadPoolExecutor1(4);
        for (int i = 0; i < 1000; i++) {
            int n = i;
            myThreadPoolExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行任务:-> " + n + " 执行的线程是:-> " + Thread.currentThread().getName());
                }
            });
        }
    }
}
